﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Recon.Portal.DataObjects;
using Recon.Portal.Business;
namespace Recon.Portal.WebUI.Modules
{
    public partial class CreditNoteEntry : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                User user = ((User)Session["CurrentUserVO"]);

                //populate remitter list
                RemitterEntity remitEntity = new RemitterEntity();
                List<Remitter> remitters = remitEntity.GetRemitterIdForMatchingCriteria(user.CustomerID);
                ddlRemitter.DataSource = remitters;
                ddlRemitter.DataBind();

                //if remitter type user then select remitter and disable selection
                if (user.UserType == "R")
                {
                    foreach (ListItem item in ddlRemitter.Items)
                    {
                        if (item.Value == user.RemitterID)
                        {
                            item.Selected = true;
                            break;
                        }
                    }
                    ddlRemitter.Enabled = false;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            User user = ((User)Session["CurrentUserVO"]);
            InvoiceEntity invEntity = new InvoiceEntity();
            Invoice inv = new Invoice
            {
                CustomerID = user.CustomerID,
                RemitterID = ddlRemitter.SelectedValue,
                InvoiceNumber = txtCreditNoteNumber.Text,
                InvoiceDate = Convert.ToDateTime(txtDate.Text),
                InvoiceDueDate = Convert.ToDateTime(txtInvoiceDueDate.Text),
                Amount = Convert.ToDouble(txtAmount.Text.Trim()) * -1, //negative amount for credit note
                PORef = txtReferenceInvoiceNumber.Text.Trim(),
                CreditNoteReason = txtReason.Text,
                InvoiceStatus = "N",
                IsPartial = false
            };

            int retval = invEntity.AddInvoice(inv);

            if (retval != 0)
            {
                lblMessage.Text = "Credit Note added successfully!";
                btnClickPopup.Show();
                ResetFields();
            }
            else
            {
                lblMessage.Text = "Error adding Credit Note!";
                btnClickPopup.Show();
            }

        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        public string ConvertDate(string yyyyMMdd)
        {
            string[] str;
            string s = yyyyMMdd;
            str = s.Split('-').ToArray();

            if (str[0].Length == 1)
            { str[0] = "0" + str[0]; }

            if (str[1].Length == 1)
            { str[1] = "0" + str[1]; }

            s = str[2] + "-" + str[1] + "-" + str[0];
            return s;

        }

        private void ResetFields()
        {
            ddlRemitter.SelectedIndex = 0;
            txtCreditNoteNumber.Text = "";
            txtDate.Text = "";
            txtAmount.Text = "";
            txtReferenceInvoiceNumber.Text = "";
            txtReason.Text = "";
        }


        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

       
    }
}